package com.macro.mall.controller;

import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dto.PmsShopCategoryParam;
import com.macro.mall.model.PmsShopCategory;
import com.macro.mall.service.PmsShopCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 店铺商品分类表(PmsShopCategory)表控制层
 *
 * @author makejava
 * @since 2021-04-27 20:10:40
 */
@RestController
@Api(tags = "PmsShopCategoryController", description = "店铺分类管理")
@RequestMapping("shopCategory")
public class PmsShopCategoryController {
    /**
     * 服务对象
     */
    @Resource
    private PmsShopCategoryService pmsShopCategoryService;

    @ApiOperation("修改商铺类别")
    @PutMapping("update/{id}")
    public CommonResult update(@Validated @RequestBody PmsShopCategoryParam param, @PathVariable Long id) {

        int count = pmsShopCategoryService.update(id, param);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("分页查询商铺分类")
    @GetMapping("list")
    public CommonResult<CommonPage<PmsShopCategory>> getList(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                             @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        List<PmsShopCategory> shopCategoryList = pmsShopCategoryService.getList(pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(shopCategoryList));
    }

    @ApiOperation("添加商铺类别")
    @PostMapping("add")
    public CommonResult add(@Validated @RequestBody PmsShopCategoryParam param) {
        System.out.println(param);
        int count = pmsShopCategoryService.add(param);
        if(count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    @ApiOperation("删除商铺类别")
    @DeleteMapping("delete/{id}")
    public CommonResult delete(@PathVariable Long id) {
        int count = pmsShopCategoryService.delete(id);
        if(count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}